var util = require('util');

exports.callback = function(req, res) {
	switch (req.body.action) {
	case 'add':
		var port = new server.Models.Port();
		port.hwAddress = req.body.hwAddress;
		port.name = req.body.name;
		port.id = req.body.id;
		port.format = req.body.format;
		port.type = req.body.type;
		port.description = req.body.description;
		port.save(function(e) {
			if (e) {
				util.log(e.message + "\n" + e.stack);
				res.end(e.message + "\n" + e.stack + "\n");
			} else
				res.redirect('https://' + req.header('X-Request-Host')
						+ '/ports/' + req.body.id);
		});
		break;
	case 'update':
		server.Models.Port.update({id : req.body.id}, {
			name : req.body.name,
			format : req.body.format,
			type : req.body.type,
			description : req.body.description
		}, function(e) {
			if (e) {
				util.log(e.message + "\n" + e.stack);
				res.end(e.message + "\n" + e.stack + "\n");
			} else
				res.redirect('https://' + req.header('X-Request-Host')
						+ '/ports/' + req.body.id);
		});
		break;
	case 'remove':
		server.Models.Port.remove({id : req.params.id}, function(e) {
			if (e) {
				util.log(e.message + "\n" + e.stack);
				res.end(e.message + "\n" + e.stack + "\n");
			} else
				res.redirect('https://' + req.header('X-Request-Host')
						+ '/ports');
		});
	}
};

exports.path = '/ports';
exports.method = 'post';
